#!/bin/bash

# ========================================
# 数据库初始化脚本
# ========================================

echo "========================================="
echo "个人任务管理系统 - 数据库初始化"
echo "========================================="

# 检查MySQL是否安装
if ! command -v mysql &> /dev/null
then
    echo "错误: 未检测到MySQL，请先安装MySQL"
    exit 1
fi

# 提示输入MySQL密码
echo ""
read -sp "请输入MySQL root密码: " MYSQL_PASSWORD
echo ""

# 执行建表脚本
echo ""
echo "正在创建数据库和表..."
mysql -u root -p"$MYSQL_PASSWORD" < src/main/resources/sql/schema.sql

if [ $? -eq 0 ]; then
    echo "✓ 数据库表创建成功！"
else
    echo "✗ 数据库表创建失败，请检查MySQL连接和脚本"
    exit 1
fi

# 询问是否插入测试数据
echo ""
read -p "是否插入测试数据？(y/n): " INSERT_TEST_DATA

if [ "$INSERT_TEST_DATA" = "y" ] || [ "$INSERT_TEST_DATA" = "Y" ]; then
    echo "正在插入测试数据..."
    mysql -u root -p"$MYSQL_PASSWORD" < src/main/resources/sql/init-data.sql
    if [ $? -eq 0 ]; then
        echo "✓ 测试数据插入成功！"
    else
        echo "✗ 测试数据插入失败"
    fi
fi

echo ""
echo "========================================="
echo "数据库初始化完成！"
echo "========================================="
echo ""
echo "下一步："
echo "1. 修改 src/main/resources/application.yml 中的数据库密码"
echo "2. 运行 mvn spring-boot:run 启动项目"
echo ""
